package org.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.demo.config.R;
import org.example.demo.entity.Student;
import org.example.demo.entity.vo.StudentQuery;
import org.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-11-24
 */
@CrossOrigin
@RestController
@RequestMapping("/demo/student")
public class StudentController {
    @Autowired
    private StudentService studentService;


    @PostMapping("student")
    public R add(@RequestBody Student student){
        studentService.save(student);
        return R.ok();
    }



//    查询全部
    @PostMapping("page/{current}/{limit}")
    public  R page(@PathVariable long current,@PathVariable long limit ){
        Page<Student> studentPage=new Page<>(current,limit);
        studentService.page(studentPage,null);
        return R.ok().data("rows",studentPage.getRecords()).data("total",studentPage.getTotal());
    }



    @ApiOperation(value = "分页老师列表")
    @GetMapping("/pageList/{page}/{limit}")
    public  R pageList(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
        Page<Student> pageParam=new Page<>(page,limit);
        studentService.page(pageParam,null);
        List<Student > records=pageParam.getRecords();//获取当前页的记录列表
        Long total=pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }



    //模糊查询
    @PostMapping("blur/{current}/{limit}")
    public R blurQuery( @ApiParam(name = "current", value = "当前页码") @PathVariable long current,
                        @ApiParam(name = "limit", value = "每页记录数") @PathVariable long limit,
                        @RequestBody(required = false) StudentQuery studentQuery ){
        Page<Student> page=new Page<>(current,limit);
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(studentQuery.getId())){
            queryWrapper.eq("id",studentQuery.getId());
        }
        if(!StringUtils.isEmpty(studentQuery.getName())){
            queryWrapper.like("name",studentQuery.getName());
        }
        queryWrapper.orderByDesc("create_Time");
        studentService.page(page,queryWrapper);
        return R.ok().data("recoed",page.getRecords()).data("total",page.getTotal());
    }



    @PostMapping("excel")
    public R addExcel(MultipartFile file){
        studentService.saveExcel(file,studentService);
        return R.ok();
    }



    @GetMapping("/export")
    public ResponseEntity<byte[]> exportToExcel() {
        byte[] excelBytes = studentService.exportToExcel();
        return ResponseEntity
                .ok()
                .header("Content-Disposition", "attachment; filename=data1.xlsx")
                .body(excelBytes);
    }



//    @PostMapping("/upload")
//    public R uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {
//
//            studentService.importData(file);
//            return R.ok();
//    }



//    excel导入数据库
    @PostMapping("/import")
    public ResponseEntity<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            studentService.importExcelData(file);
            return ResponseEntity.ok("Excel data imported successfully");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to import Excel data");
        }
    }



    @DeleteMapping("delete/{id}")
        public  R delete(@PathVariable int id){
            studentService.removeById(id);
            return R.ok();
    }



}
















